<?php
namespace Dybasedev\Dataman\Convert;

use Closure;

class Converter
{
    protected $originTable;

    protected $targetTable;

    protected $connection;

    protected $originColumns;

    protected $targetColumns;

    protected $fillColumns;

    protected $triggers;

    protected $convertUnit;

    public function __construct($connection)
    {
        $this->connection = $connection;
    }

    public function setOriginTable($origin)
    {
        $this->originTable = $origin;
    }

    public function setTargetTable($target)
    {
        $this->targetTable = $target;
    }

    public function from($column)
    {
        $this->originColumns[] = $column;

        return $this;
    }

    public function to($column, Closure $callback = null)
    {
        $this->targetColumns[] = [$column, $callback];

        return $this;
    }

    public function fill($column, $value)
    {
        $this->fillColumns[] = [$column, $value];

        return $this;
    }

    public function trigger(Closure $callback)
    {
        $this->triggers[] = $callback;
    }

    public function addColumn($column, $direct = true)
    {

    }

    public function build(Closure $callback = null)
    {
        
        $this->connection->table($this->originTable)->chunk(50, function($data) use ($callback) {
            foreach ($data as $row) {

                if ($callback('convert.start', $row) === false) {
                    continue;
                }

                $class = $this;

                // 开启数据库事务保证转换过程中一些触发不会产生遗留数据
                // 该特性需要数据库本身支持，如MySQL必须是InnoDB或其他支持事务的数据库引擎
                $this->connection->transaction(function() use ($callback, $row, $class) {
                    $add = [];

                    call_user_func($callback, 'convert.transaction.start', $row);

                    // 依次读取来源数据
                    foreach ($this->originColumns as $key => $column) {
                        $item = $row[$column];

                        if (!is_null($this->targetColumns[$key][1])) {
                            $item = call_user_func($this->targetColumns[$key][1], $item);
                        }

                        $add[$this->targetColumns[$key][0]] = $item;
                    }

                    foreach ($this->triggers as $trigger) {
                        call_user_func($trigger, $class, $add, $row);
                    }

                    // 创建填充字段
                    foreach ($this->fillColumns as $fill) {
                        if ($fill[1] instanceof Closure) {
                            $add[$fill[0]] = call_user_func($fill[1], $add, $row);
                            continue;
                        }
                        $add[$fill[0]] = $fill[1];
                    }

                    $callback('convert.result', $add);

                    $this->connection->table($this->targetTable)->insert($add);
                });
            }
        });
    }
    
    public function getConnection()
    {
        return $this->connection;
    }

    // public function from($column)
    // {
    //     $unit = new ConvertUnit();

    //     $unit->setOrigin($column, ConvertUnit::FIELD);

    //     return $this->convertUnit[] = $unit;
    // }

    // public function to($column, Closure $callback)
    // {
    //     $unit = end($this->convertUnit);

    //     $unit->setTarget($column, )
    // }
}